package cn.changeforyou.web.dao.mybatis.ext.extBuilder.writer;

import cn.changeforyou.web.dao.mybatis.ext.extBuilder.SqlContext;
import cn.changeforyou.web.dao.mybatis.ext.extBuilder.sqlUtils.ColumnsUtils;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.FieldMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.MappedStatementResultType;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhyu
 * @version 1.0
 * @date 2022/3/4 12:32
 */
@Component
public class SelectSimpleSqlWriter extends AbstractSqlWriter{

    @Override
    void doWriteSql(SqlContext context) {
       SqlBuilder sql = context.getSql();
        sql.append(" SELECT");

        List<FieldMetaInfo> selectMetaInfo = context.getSelectMetaInfo();
        if(context.getMappedStatementResultType() == MappedStatementResultType.count) {
            sql.append(" count(1)");
        }else {
            for (int i = 0; i < selectMetaInfo.size(); i++) {
                if(i != 0) {
                    sql.append(",");
                }
                ColumnsUtils.writeColumnsInSelectSentence(sql, selectMetaInfo.get(i));
            }
        }
    }

    @Override
    public SentenceEnum getSentenceEnum() {
        return SentenceEnum.select;
    }
}
